﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using AT_UHF_NET;

namespace ceTerm
{
    public partial class ManualEtiketleme : XForm
    {
        public ManualEtiketleme()
        {
            InitializeComponent();
            pnlSinyalGucu.Visible = false;

            this.WindowState = FormWindowState.Maximized;
            this.KeyPreview = true;

            Terminal.SetActivatedForm(this);

            timer.Interval = (int)Settings.OkumaSuresiMs;

            chkRaf.CheckStateChanged += new EventHandler(chkRaf_CheckStateChanged);
        }

        DataTable dtRacks;
        void chkRaf_CheckStateChanged(object sender, EventArgs e)
        {
            if (chkRaf.CheckState == CheckState.Checked)
            {
                try
                {
                    //dtRacks.Clear();
                    dtRacks = Main.ceWS.GetRacks(Main.Password);
                }
                catch (Exception ex)
                {
                    ShowException("Veritabanı hatası!", "Hata", ex);
                    this.Close();
                }

                if (dtRacks.Select("ShelfId >= " + intParse(txtBarkod.Text).ToString(), string.Empty).Length > 0)
                {
                    DataRow dr = dtRacks.Select("ShelfId >= " + intParse(txtBarkod.Text).ToString(), "ShelfId")[0];
                    ShelfId = intParse(dr["ShelfId"]);
                    Shelf = dr["Name"].ToString();

                }
                txtBarkod.Text = ShelfId.ToString();
                lblShelf.Text = Shelf;
            }
            else
                lblShelf.Text = string.Empty;
        }
            int ShelfId = 0;
            string Shelf = string.Empty;

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F17: // RButton
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger 
                    btnWrite_Click(null, null);
                    break;

                default:
                    break;
            }
            base.OnKeyDown(e);
        }


        private void ShowInfo(string info)
        {
            lblStatus.Text = info;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            Terminal.StopRFID();

            if (tags.Count == 0)
            {
                ShowInfo("Ortamda RFID etiket yok, yazılamadı!");
                Terminal.PlayFail();
                tags.Clear();
                return;
            }
            else if (tags.Count > 1)
            {
                ShowInfo("Ortamda birden fazla (" + tags.Count + ") RFID etiket var, yazılamadı!");
                Terminal.PlayFail();
                tags.Clear();
                return;
            }
            else
                action = Action.Write;
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            tags.Clear();
            Terminal.StartRFID();
            timer.Enabled = true;
        }

        //IUHFHost Members
        List<string> tags = new List<string>();

        public override void GetAccessEPC(string EPC)
        {
            string[] temp = EPC.Split(",".ToCharArray());

            EPC = temp[0];

            if (tags.IndexOf(EPC) < 0)
                tags.Add(EPC);
        }

        public override void GetReply(string Reply)
        {
            switch (action)
            {
                case Action.Write:
                    int EPCint = intParse(txtBarkod.Text);
                    if (EPCint < 1)
                        return;

                    if (Terminal.WriteRFID(EPCint))
                    {
                        ShowInfo("RFID etikete yazıldı. (" + EPCint.ToString() + ")");
                        if (chkRaf.CheckState == CheckState.Checked)
                        {
                            if (dtRacks.Select("ShelfId > " + intParse(txtBarkod.Text).ToString(), string.Empty).Length > 0)
                            {
                                DataRow dr = dtRacks.Select("ShelfId > " + intParse(txtBarkod.Text).ToString(), "ShelfId")[0];
                                ShelfId = intParse(dr["ShelfId"]);
                                Shelf = dr["Name"].ToString();

                            }
                            txtBarkod.Text = ShelfId.ToString();
                            lblShelf.Text = Shelf;
                        }
                        else
                        {
                            if (EPCint > 0)
                                txtBarkod.Text = (EPCint + 1).ToString();
                        }
                           
                        Terminal.PlaySuccess();
                    }
                    else
                    {
                        ShowInfo("RFID etikete yazılamadı!" /*+ result.ToString()*/);
                        Terminal.PlayFail();
                    }

                    tags.Clear();
                    break;

                case Action.SetPower:
                    bool powerResult = Terminal.RFID.SET_PowerControl((uint)power.Value);
                    Terminal.StartRFID();
                    break;   
            }
            action = Action.IDLE;
        }


        //private void timerInterval_ValueChanged(object sender, EventArgs e)
        //{
        //    timer.Interval = (int)Settings.OkumaSuresiMs;
        //}

        //private void power_ValueChanged(object sender, EventArgs e)
        //{
        //    action = Action.SetPower;
        //}
    }
}